package com.heihei.config;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.heihei.operator.UserInfoOperator;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.time.LocalDateTime;

/**
 *  自动填充公共字段
 *
 * @author duanhf
 * 2021年11月25日 17:57:41
 */
@Component
public class CommonFieldFillConfiguration implements MetaObjectHandler {

    @Resource
    private UserInfoOperator userInfoOperator;

    @Override
    public void insertFill(MetaObject metaObject) {
        String currUserName = this.userInfoOperator.getUserName();
        LocalDateTime now = LocalDateTime.now();
        this.setInsertFieldValByName("createUser", currUserName, metaObject);
        this.setInsertFieldValByName("updateUser", currUserName, metaObject);
        this.setInsertFieldValByName("createTime", now, metaObject);
        this.setInsertFieldValByName("updateTime", now, metaObject);
        this.setInsertFieldValByName("delFlag", 0, metaObject);

    }

    @Override
    public void updateFill(MetaObject metaObject) {
        String currUserName = this.userInfoOperator.getUserName();
        LocalDateTime now = LocalDateTime.now();
        this.setUpdateFieldValByName("updateUser", currUserName, metaObject);
        this.setUpdateFieldValByName("updateTime", now, metaObject);
        this.setUpdateFieldValByName("delFlag", 0, metaObject);
    }
}
